import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Headers, Http} from '@angular/http';
import {Fault} from '../entity/fault.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class FaultService {
  private readonly param = 'faults';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  getFaults(): Promise<Fault[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as Fault[])
      .catch(this.handleError);
  }

  /**
   * 根据型号获得故障信息
   *
   * @param {number} modelId
   * @returns {Promise<Fault[]>}
   * @memberof FaultService
   */
  getFaultsByModelId(modelId: number): Promise<Fault[]> {
    return this.http.get(`${this.url}/modelId/${modelId}`)
      .toPromise()
      .then(response => response.data as Fault[])
      .catch(this.handleError);
  }

  /**
   * 根据schemeId获得故障信息
   *
   * @param {number} modelId
   * @returns {Promise<Fault[]>}
   * @memberof FaultService
   */
  getFaultsBySchemeId(schemeId: number): Promise<Fault[]> {
    return this.http.get(`${this.url}/schemeId/${schemeId}`)
      .toPromise()
      .then(response => response.data as Fault[])
      .catch(this.handleError);
  }

  create(fault: Fault): Promise<boolean> {
    return this.http.post(this.url, JSON.stringify(fault))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  update(fault: Fault): Promise<Fault> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(fault))
      .toPromise()
      .then(() => fault)
      .catch(this.handleError);
  }

  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }

  batchDelete(valueIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  faultCodeService(faultCode: string, schemeId: number, faultId: number) {
    const url = `${this.url}/faultCode/${faultCode}/schemeId/${schemeId}/faultId/${faultId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  faultDescService(faultDesc: string, schemeId: number, faultId: number) {
    const url = `${this.url}/faultDesc/${faultDesc}/schemeId/${schemeId}/faultId/${faultId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }


}
